home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Program: CShell
- ** File: file2.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1991 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "CShell.h" /* Get the CShell includes/typedefs, etc. */
- #include "CShellCommon.h" /* Get the stuff in common with rez. */
- #include "CShell.protos" /* Get the prototypes for CShell. */
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __UTILITIES__
- #include "Utilities.h"
- #endif
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* AppFreeDocument
- **
- ** Frees up any application-specific memory in the document. The document
- ** may have a bunch of handles off the main handle of the document. This
- ** is where they are freed. AppDisposeDocument calls this prior to releasing
- ** the ram for the main handle of the document, so release everything else
- ** here, or you will have a memory leak.
- */
-
- #pragma segment File
- void AppFreeDocument(FileRecHndl frHndl)
- {
- Handle textHndl;
-
- if (textHndl = (*frHndl)->doc.textHndl) {
- DisposHandle(textHndl);
- (*frHndl)->doc.textHndl = nil;
- }
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* AppInitDocument
- **
- ** Do any additional document initialization here.
- */
-
- #pragma segment File
- OSErr AppInitDocument(FileRecHndl frHndl)
- {
- (*frHndl)->doc.version = kVersion;
- (*frHndl)->doc.printRecValid = false;
- (*frHndl)->doc.connected = false;
- (*frHndl)->doc.textHndl = nil;
-
- return(noErr);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* AppReadDocument
- **
- ** Reads in specified file into the data portion of a document handle.
- */
-
- #pragma segment File
- OSErr AppReadDocument(FileRecHndl frHndl)
- {
- short fileRefNum;
- OSErr err;
- char hstate;
- Ptr ptr1, ptr2;
- long count;
- Handle textHndl;
-
- fileRefNum = (*frHndl)->fileState.refNum;
-
- err = SetFPos(fileRefNum, fsFromStart, 0);
- /* Set the file position to the beginning of the file. */
-
- if (!err) { /* Read board info from file. */
- hstate = LockHandleHigh((Handle)frHndl);
- ptr1 = (Ptr)&((*frHndl)->doc);
- ptr2 = (Ptr)&((*frHndl)->doc.endFileInfo);
- count = (long)ptr2 - (long)ptr1;
- err = FSRead(fileRefNum, &count, ptr1);
- HSetState((Handle)frHndl, hstate);
- if ((*frHndl)->doc.version != kVersion)
- err = kWrongVersion;
- }
-
- if (!err) { /* Read TextEdit text from file. */
- textHndl = NewHandle(32768);
- if (!(err = MemError())) {
- count = 32768;
- /* The size of the text isn't saved to disk. This is the maximum
- ** that a TextEdit record can accept. */
- hstate = LockHandleHigh(textHndl);
- err = FSRead(fileRefNum, &count, *textHndl);
- HSetState(textHndl, hstate);
- if (err == eofErr) err = noErr;
- if (err) count = 0;
- SetHandleSize(textHndl, count);
- /* Set the handle to the actual size of the text on disk. */
- (*frHndl)->doc.textHndl = textHndl;
- }
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* AppWriteDocument
- **
- ** Writes the data portion of a document handle to the specified file.
- */
-
- #pragma segment File
- OSErr AppWriteDocument(FileRecHndl frHndl)
- {
- short fileRefNum;
- OSErr err;
- char hstate;
- Ptr ptr1, ptr2;
- long count, fpos;
- TEHandle te;
- Handle textHndl;
-
- fileRefNum = (*frHndl)->fileState.refNum;
-
- err = SetFPos(fileRefNum, fsFromStart, 0);
- /* Set the file position to the beginning of the file. */
-
- if (!err) { /* Write board info to file. */
- hstate = LockHandleHigh((Handle)frHndl);
- ptr1 = (Ptr)&((*frHndl)->doc);
- ptr2 = (Ptr)&((*frHndl)->doc.endFileInfo);
- count = (long)ptr2 - (long)ptr1;
- err = FSWrite(fileRefNum, &count, ptr1);
- HSetState((Handle)frHndl, hstate);
- }
-
- if (!err) { /* Write out-box TextEdit control text to file. */
- te = (*frHndl)->doc.outBox;
- textHndl = (*te)->hText;
- count = (*te)->teLength;
- hstate = LockHandleHigh(textHndl);
- err = FSWrite(fileRefNum, &count, *textHndl);
- HSetState(textHndl, hstate);
- }
-
- if (!err) {
- err = GetFPos(fileRefNum, &fpos);
- if (!err) err = SetEOF(fileRefNum, fpos);
- }
-
- return(err);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- #pragma segment File
- OSErr AppDuplicateDocument(FileRecHndl oldFrHndl, FileRecHndl *newFrHndl)
- {
- OSErr err;
- TEHandle te;
- Handle oldHndl, newHndl;
- long size;
-
- err = AppNewDocument(newFrHndl);
- if (!err) {
- te = (*oldFrHndl)->doc.outBox;
- oldHndl = (*te)->hText;
- size = (*te)->teLength;
- if (newHndl = NewHandle(size)) {
- (**newFrHndl)->doc.textHndl = newHndl;
- BlockMove(*oldHndl, *newHndl, size);
- }
- else err = memFullErr;
- }
- return(err);
- }
-
-
-
-